package com.test.dao.impl;

import com.test.dao.DishDao;
import com.test.domain.Dish;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Repository
public class DishDaoImpl implements DishDao {

    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public void insertDish(Dish dish) {
        String temp = dish.getMerchantPhoneNumber();
        String dishNumber= System.currentTimeMillis() +temp.substring(temp.length() - 7);
        System.out.println(dishNumber);

        String sql = "insert into dish values(?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,dishNumber,dish.getDishName(),dish.getDishDescription(),dish.getDishPrice(),
                dish.getDishGrade(),0,0,dish.getMerchantPhoneNumber(),0);
    }

    @Override
    public List<Dish> getDishListByPhoneNumber(String phoneNumber) {
        String sql = "select * from dish where merchant_phone_number=?";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Dish>(Dish.class),phoneNumber);
    }

    @Override
    public void updateDishName(String dishNumber, String dishName) {
        String sql = "update dish set dish_name=? where dish_number=?";
        jdbcTemplate.update(sql,dishName,dishNumber);

    }

    @Override
    public void updateDishDescription(String dishNumber, String dishDescription) {
        String sql = "update dish set dish_description=? where dish_number=?";
        jdbcTemplate.update(sql,dishDescription,dishNumber);
    }

    @Override
    public void updateDishPrice(String dishNumber, BigDecimal dishPrice) {
        String sql = "update dish set dish_price=? where dish_number=?";
        jdbcTemplate.update(sql,dishPrice,dishNumber);
    }

    @Override
    public void updateIsSale(String dishNumber, int isSale) {
        String sql = "update dish set is_sale=? where dish_number=?";
        jdbcTemplate.update(sql,isSale,dishNumber);
    }

    @Override
    public void deleteDishByDishNumber(String dishNumber) {
        String sql="delete from dish where dish_number=?";
        jdbcTemplate.update(sql,dishNumber);
    }

    @Override
    public List<Dish> getDishListByMerchantPhoneNumberAndKeyword(String merchantPhoneNumber, String keyword) {
        String dishSql = "select * from dish where merchant_phone_number=? and dish_name like ?";
        return jdbcTemplate.query(dishSql,new BeanPropertyRowMapper<Dish>(Dish.class),
                merchantPhoneNumber,"%"+keyword+"%");
    }

    @Override
    public String getMerchantPhoneByDishNumber(String DishNumber) {
        String sql="select merchant_phone_number from dish where dish_number = ?";
        return jdbcTemplate.queryForObject(sql,String.class,DishNumber);
    }

    @Override
    public List<String> getListByRecommend() {
        String sql="select distinct merchant_phone_number from dish where is_recommend=1";
        return jdbcTemplate.queryForList(sql,String.class);
    }
}
